﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AgileDashboard.Data;
using AgileDashboard.Entities;
using AgileDashboard.Services.Helpers;
using tfs = Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace AgileDashboard.Services
{
    public class WorkItemService
    {
        
        private WorkItemDataSource workItemDataSource { get; set; }
        private QueryDataSource queryDataSource { get; set; }
        private TfsDataLoader tfsDataLoader { get; set; }
        private LocalDataLoader localDataLoader { get; set; }



        public WorkItemService()
        {
            tfsDataLoader = new TfsDataLoader();
            workItemDataSource = new WorkItemDataSource();
            queryDataSource = new QueryDataSource();
            localDataLoader = new LocalDataLoader();
        }



        public List<WorkItem> GetWorkItems(Guid queryId, int projectId, bool recursive)
        {
            tfs.Project proj = tfsDataLoader.GetTeamProjectById(projectId);
            string queryText = queryDataSource.GetQueryTextFromQuery(queryId, proj);



            return workItemDataSource.GetWorkItemsfromQuery(queryText, proj, recursive);

        }


        public List<VoteDetail> GetVoteDetails(int id)
        {
            return localDataLoader.GetVoteDetailsfromLocalStore(id);
        }

      


        public void SyncWorkItems(List<WorkItem> workItems)
        {


            PlanningPokerContext db = new PlanningPokerContext();

            foreach (WorkItem wi in workItems)
            {
                WorkItem fromDB = db.WorkItems.Find(wi.Id);
                if (fromDB == null)
                    db.WorkItems.Add(wi);
               


            }

            db.SaveChanges();

            

        }











    



    }
}
